<?PHP
// vim: set expandtab tabstop=4 shiftwidth=4:
// +----------------------------------------------------------------------+
// | SAPID CMS                               		  					  |
// +----------------------------------------------------------------------+
// | Author:  Dmitry Sheiko (www.cmsdevelopment.com)	                  |
// | Copyright (c) 2004 Dmitry Sheiko	                                  |
// | http://sapid.sourceforge.net	                                      |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+
// Release: 08.11.04 (dd/mm/yy)
// $Id: sapi_expressions.inc.php,v 1.9 2006/01/16 09:44:11 sheiko Exp $

/**
 * Return value of a variable
 * 
 * @param string $var
 * @return string $value
 */
function valueof($var){
	return $GLOBALS["env"][$var];
}

/**
 * Generate confirmation code
 * 
 * @param string $var
 * @param string $md5ofvar
 */
function gencode($var,$md5ofvar){
	srand(round(microtime(1)*10000));
	$code= substr(md5(rand(1,100)),0,5);
	$GLOBALS["env"][$var]=$code;
	$GLOBALS["env"][$md5ofvar]=md5($code);
}
/**
 * Specify value to variable
 * 
 * @param string $var
 * @param string $value
 */
function set($var, $value){
	$GLOBALS["env"][$var]=$value;
}
/**
 * Show string depend upon first argument
 * 
 * @param string $var
 * @param string $value
 * @return string 
 */
function isnotnull($var, $val1=false, $val2=false) {
	if(isset($GLOBALS["env"][$var]) AND $GLOBALS["env"][$var]) return $val1; else return $val2;
}
/**
 * Show date in specified format
 * 
 * @param string $Format
 * @param string $Date in source format
 * @return string 
 */
function datef($Format, $DateTime) {
	$DateElems = split("\/", substr($DateTime,0,10));
	$TimeElems = split(":", substr($DateTime,11));
	return date($Format, mktime($TimeElems[0],$TimeElems[1], $TimeElems[2], $DateElems[1], $DateElems[0], $DateElems[2]));
}
/**
 * Enumeration expression
 * Show value through index
 * 
 * @param string $index
 * @param string $value
 * ..
 * @return string 
 */
function enum() {
	$index = func_get_arg(0);
	return func_get_arg($index+1);
}
/**
 * Return true if row is sorted by ascend. Each one is more or equal next one
 * 
 * @param string $value
 * ..
 * @return string 
 */
function lt() {
	for($i=func_num_args()-1;$i>0;$i--) {
		if(func_get_arg($i)<=func_get_arg($i-1)) return false;
	}
	return true;
}
/**
 * Return true if row is sorted by ascend. Each one is more than next one
 * 
 * @param string $value
 * ..
 * @return string 
 */
function leq() {
	for($i=func_num_args()-1;$i>0;$i--) {
		if(func_get_arg($i)<func_get_arg($i-1)) return false;
	}
	return true;
}
/**
 * Return true if row is sorted by descend. Each one is less or equal next one
 * 
 * @param string $value
 * ..
 * @return string 
 */
function gt() {
	for($i=func_num_args()-1;$i>0;$i--) {
		if(func_get_arg($i)>=func_get_arg($i-1)) return false;
	}
	return true;
}
/**
 * Return true if row is sorted by descend. Each one is less than next one
 * 
 * @param string $value
 * ..
 * @return string 
 */
function geq() {
	for($i=func_num_args()-1;$i>0;$i--) {
		if(func_get_arg($i)>func_get_arg($i-1)) return false;
	}
	return true;
}
/**
 * Return true if row contains equal values. Each one is equal next one
 * 
 * @param string $value
 * ..
 * @return string 
 */
function eq() {
	for($i=func_num_args()-1;$i>0;$i--) {
		if(func_get_arg($i)!=func_get_arg($i-1)) return false;
	}
	return true;
}
/**
 * Return true if row contains not equal values. Each one is not equal next one
 * 
 * @param string $value
 * ..
 * @return string 
 */
function neq() {
	for($i=func_num_args()-1;$i>0;$i--) {
		if(func_get_arg($i)==func_get_arg($i-1)) return false;
	}
	return true;
}
/**
 * Return sum of argumets values
 * 
 * @param string $value
 * ..
 * @return string 
 */
function add() {
	$res = false;
	for($i=0;$i<func_num_args();$i++) {
		if(gettype(func_get_arg($i))=="string")
			$res.=func_get_arg($i);
		else
			$res+=func_get_arg($i);
	}
	return $res;
}
/**
 * Return odds of argumets values
 * 
 * @param string $value
 * ..
 * @return string 
 */
function subtract() {
	$res = func_get_arg(0);
	for($i=1;$i<func_num_args();$i++) {
		$res-=func_get_arg($i);
	}
	return $res;
}
/**
 * Return composition of argumets values
 * 
 * @param string $value
 * ..
 * @return string 
 */
function multiply() {
	$res = func_get_arg(0);
	for($i=1;$i<func_num_args();$i++) {
		$res*=func_get_arg($i);
	}
	return $res;
}
/**
 * Return quotient of argumets values
 * 
 * @param string $value
 * ..
 * @return string 
 */
function divide($a1=false, $a2=false, $a3=false, $a4=false, $a5=false, $a6=false, $a7=false, $a8=false, $a9=false, $a10=false) {
	$res = func_get_arg(0);
	for($i=1;$i<func_num_args();$i++) {
		$res/=func_get_arg($i);
	}
	return $res;
}
/**
 * Translate phrase to specified lanuage
 * 
 * @param string Source phrase
 * @param string Traget lanuage
 * @return string 
 */
function mlstring($mlstring_code, $mlstring_lang="auto") {

	global $env, $sapi_obj;

	if($mlstring_lang=="" OR $mlstring_lang=="auto") {
		$mlstring_lang=$env["argv"][0];
	};
	
	$langfile=$env["root_path"]."lang/".$mlstring_lang.".inc.php";
	if(!file_exists($langfile)) {
		$sapi_obj->error = "Language file $langfile does not exists";
		return;
	};

	$lang_phpcode=file_get_contents($langfile);

	$tags=array("<?PHP","<?php","<?","?>");
	$lang_phpcode=str_replace($tags,"",$lang_phpcode);

	//adding $http_path variable to language PHP file because language file uses $http_path and there's no definition of $http_path
	$lang_phpcode="\$http_path='".$env["http_path"]."';\n".$lang_phpcode;

	eval($lang_phpcode); //Here we are executing code, the result of this code is $lang array which contains key=>translation pairs

	$lang_output="";

	$lang_output=array_merge($lang_output,$lang);

	unset($lang);



	if($langdir=@opendir($env["root_path"]."lang")) {

		while( ($langfile=readdir($langdir)) !== false ) {

			if($langfile!="." && $langfile!=".." && !is_dir($env["root_path"]."lang/".$langfile) && preg_match("/^".$mlstring_lang.".+.inc.php$/s",$langfile)) {

				//Here we are doing almost the same as above, but with extra language files

				$lang_phpcode=file_get_contents($env["root_path"]."lang/".$langfile);

				$tags=array("<?PHP","<?php","<?","?>");
				$lang_phpcode=str_replace($tags,"",$lang_phpcode);
				$lang_phpcode="\$http_path='".$env["http_path"]."';\n".$lang_phpcode;
				eval($lang_phpcode);

				$lang_output=array_merge($lang_output,$lang);

				unset($lang);

			};

		};

		closedir($langdir);

	};



	return $lang_output[$mlstring_code];
}
?>